<script setup lang="ts">
import { ref, watch } from "vue";
import { onLoad, onPageScroll, onPullDownRefresh, onReachBottom } from "@dcloudio/uni-app";
import { useInfiniteScroll } from "@/composables/useInfiniteScroll";
import { getInviteList } from "@/api/user";

const { items: list, isLoading, hasMore, error, resetAndLoad, loadMore } = useInfiniteScroll(getInviteList);

/** 查询参数  */
const queryParams = ref({});

watch(queryParams, (newQueryParams) => {
  console.log("Query Params changed:", newQueryParams);
  resetAndLoad(newQueryParams);
}, { deep: true });

// 页面加载
onLoad(() => {
  resetAndLoad(queryParams.value);
});

// 监听用户下拉动作
onPullDownRefresh(() => {
  resetAndLoad(queryParams.value).finally(() => {
    // 停止当前页面下拉刷新
    uni.stopPullDownRefresh();
  });
});

// 页面滚动到底部的事件
onReachBottom(() => loadMore());

// 吸顶变色
const isScrolled = ref(false);
onPageScroll((e)=>{
  isScrolled.value = e.scrollTop > 0;
});
</script>

<template>
<view class="relative overflow-hidden">
  <page-navbar title="邀请记录" :hideTitle="true" :isScrolled="isScrolled"></page-navbar>

  <page-title title="邀请记录"></page-title>

  <page-list :list="list" :hasMore="hasMore" :isLoading="isLoading" :loadMore="loadMore">
    <view
      class="mx-4 mt-3 p-3 rounded-lg border-(~ solid white) grid grid-cols-[auto_1fr] items-center gap-2 bg-#EFF6FF"
      v-for="item in list" :key="item.id"
    >
      <image class="size-10 align-middle rounded" :src="item.user.avatar" :fade-in="true" :lazy-load="true" mode="widthFix" />
      <view class="text-(sm #333333)">{{item.user.nickname}}</view>
    </view>
  </page-list>
</view>
</template>
